﻿//-----------------------------------------------------------------------
// <copyright file="PivotXmlBuilder.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Library
{
    using System.Globalization;
    using PivotCollectionTools.Data;
    using PivotCollectionTools.Xml;

    /// <summary>
    /// Builds <see cref="PivotXml"/> object from a <see cref="PivotTable"/> object.
    /// </summary>
    public class PivotXmlBuilder
    {
        /// <summary>
        /// Gets or sets the image base.
        /// </summary>
        /// <value>The image base of the Deep Zoom images for the Pivot Collection XML.</value>
        public string ImageBase { get; set; }

        /// <summary>
        /// Gets or sets the collection title.
        /// </summary>
        /// <value>The collection title.</value>
        public string CollectionTitle { get; set; }

        /// <summary>
        /// Creates the XML from specified <see cref="PivotTable"/>.
        /// </summary>
        /// <param name="table">The specified table to create the XML from.</param>
        /// <returns>
        /// A <see cref="PivotXml"/> based on the <see cref="PivotTable"/> specified.
        /// </returns>
        public PivotXml CreatePivotXml(PivotTable table)
        {
            PivotXml xml = new PivotXml();
            xml.AddItemList(this.ImageBase);
            xml.Name = this.CollectionTitle;

            foreach (var category in table.Categories)
            {
                if (category.Name == "Item Attribute")
                {
                    continue;
                }

                switch (category.ValueType)
                {
                    case PivotTableCellType.Text:
                        xml.AddCategory(new FacetCategory(category.Name, FacetDataType.String, category.IsFilterVisible, category.IsWordWheelVisible, category.IsMetadataVisible));
                        break;
                    case PivotTableCellType.Number:
                        xml.AddCategory(new FacetCategory(category.Name, FacetDataType.Number, category.IsFilterVisible, category.IsWordWheelVisible, category.IsMetadataVisible));
                        break;
                    default:
                        xml.AddCategory(new FacetCategory(category.Name, FacetDataType.String, category.IsFilterVisible, category.IsWordWheelVisible, category.IsMetadataVisible));
                        break;
                }
            }

            int counter = 0;
            foreach (var dataRow in table.DataRows)
            {
                var newItem = new Item()
                {
                    Name = dataRow["Name"].Value,

                    Identifier = counter.ToString(CultureInfo.InvariantCulture),
                    Image = string.Concat("#", counter.ToString(CultureInfo.InvariantCulture))
                };

                if (!string.IsNullOrEmpty(dataRow["Href"].Value))
                {
                    newItem.Href = dataRow["Href"].Value;
                }

                if (!string.IsNullOrEmpty(dataRow["Description"].Value))
                {
                    newItem.Description = dataRow["Description"].Value;
                }

                foreach (var cell in dataRow.Cells)
                {
                    if (string.IsNullOrEmpty(cell.Value))
                    {
                        continue;
                    }

                    if (cell.Column.Category.DataBound)
                    {
                        switch (cell.Column.Category.ValueType)
                        {
                            case PivotTableCellType.Text:
                                newItem.Facets.AddTextFacet(cell.Column.Category.Name, cell.Value);
                                break;
                            case PivotTableCellType.Number:
                                newItem.Facets.AddNumberFacet(cell.Column.Category.Name, decimal.Parse(cell.Value, NumberStyles.Any, CultureInfo.InvariantCulture));
                                break;
                        }
                    }
                }

                xml.ItemList[0].Items.Add(newItem);
                counter++;
            }

            return xml;
        }
    }
}
